package com.yy.market.mvp.ui;


import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.recker.flybanner.FlyBanner;
import com.yy.market.R;
import com.yy.market.mvp.adapter.HomeAdapter;
import com.yy.market.mvp.bean.HomeTypeButton;
import com.yy.market.mvp.data.HomeData;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements XRecyclerView.LoadingListener {

    private XRecyclerView recyclerView;
    private FlyBanner banner;
    private HomeAdapter homeAdapter;
    private ArrayList<HomeData> homeDatas = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void initView(LayoutInflater inflater, ViewGroup container, View view) {
        recyclerView = (XRecyclerView) view.findViewById(R.id.home_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLoadingListener(this);
        recyclerView.setLoadingMoreEnabled(false);
        homeAdapter = new HomeAdapter(homeDatas,this.context);
        recyclerView.setAdapter(homeAdapter);

        // banner
        View bannerView = inflater.inflate(R.layout.home_banner_view, container, false);
        initBannerView(bannerView);
        recyclerView.addHeaderView(bannerView);

        // 10个button
        HomeData tenBtnHomeData = new HomeData();
        tenBtnHomeData.setType(HomeData.HOME_TYPE_10_BUTTON);
        int[] buttonImages = {R.drawable.temp_home_icon_1,
                R.drawable.temp_home_icon_2,
                R.drawable.temp_home_icon_3,
                R.drawable.temp_home_icon_4,
                R.drawable.temp_home_icon_5,
                R.drawable.temp_home_icon_6,
                R.drawable.temp_home_icon_7,
                R.drawable.temp_home_icon_8,
                R.drawable.temp_home_icon_9,
                R.drawable.temp_home_icon_10};
        String[] buttonTitles = {
                "超市日用",
                "水果",
                "休闲零食",
                "粮油调味",
                "鲜花蛋糕",
                "美食外卖",
                "水果生鲜",
                "品牌街坊",
                "母婴用品",
                "化妆品"
        };
        List<HomeTypeButton> homeTypeButtons = new ArrayList<>();
        for (int i=0;i<buttonTitles.length;i++) {
            HomeTypeButton homeTypeButton = new HomeTypeButton();
            homeTypeButton.setButtonTitle(buttonTitles[i]);
            homeTypeButton.setButtonImage(buttonImages[i]);
            homeTypeButtons.add(homeTypeButton);
        }
        tenBtnHomeData.setData(homeTypeButtons);
        homeDatas.add(tenBtnHomeData);

        HomeData spec1 = new HomeData();
        spec1.setType(HomeData.HOME_TYPE_SPEC);
        homeDatas.add(spec1);

        HomeData ad1 = new HomeData();
        ad1.setType(HomeData.HOME_TYPE_AD_VIEW);
        List<String> ads = new ArrayList<>();
        ads.add("1. 非常，惊喜等着你");
        ads.add("2. 海报换礼品，剪角来就送!开心圣诞节!");
        ads.add("3. 华丽圣诞狂响曲，喜迎新春献贺礼");
        ads.add("4. 欢乐节日*劲爆优惠大行动!");
        ad1.setData(ads);
        homeDatas.add(ad1);

        HomeData spec2 = new HomeData();
        spec2.setType(HomeData.HOME_TYPE_SPEC);
        spec2.setData(5.0f);
        homeDatas.add(spec2);

        homeAdapter.notifyDataSetChanged();

    }

    private void initBannerView(View bannerView) {
        banner = (FlyBanner) bannerView.findViewById(R.id.banner_view_pager);
        banner.setPoinstPosition(FlyBanner.RIGHT);
        List<Integer> imageUrl = new ArrayList();
        imageUrl.add(R.drawable.temp_home_banner_1);
        imageUrl.add(R.drawable.temp_home_banner_2);
        imageUrl.add(R.drawable.temp_home_banner_3);
        banner.setImages(imageUrl);
    }

    @Override
    public void onRefresh() {
        new AsyncTask() {

            @Override
            protected Object doInBackground(Object[] params) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
                recyclerView.refreshComplete();
            }
        }.execute();
    }

    @Override
    public void onLoadMore() {

    }
}
